/*
 #
 #  Files       : Results.h
 #                ( C++ header file )
 #
 #  Description : The SmallMatrix Library
 #                ( http://code.google.com/p/smallmatrix )
 #
 #  Copyright   : Olivier Juan
 #                ( http://www.mas.ecp.fr/vision/Personnel/juan/ )
 #
 #  License     : CeCILL-C
 #                ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html )
 #
 #  This software is governed by the CeCILL-C license under French law and
 #  abiding by the rules of distribution of free software.  You can  use,
 #  modify and or redistribute the software under the terms of the CeCILL-C
 #  license as circulated by CEA, CNRS and INRIA at the following URL
 #  "http://www.cecill.info".
 #
 #  As a counterpart to the access to the source code and  rights to copy,
 #  modify and redistribute granted by the license, users are provided only
 #  with a limited warranty  and the software's author,  the holder of the
 #  economic rights,  and the successive licensors  have only  limited
 #  liability.
 #
 #  In this respect, the user's attention is drawn to the risks associated
 #  with loading,  using,  modifying and/or developing or reproducing the
 #  software by the user in light of its specific status of free software,
 #  that may mean  that it is complicated to manipulate,  and  that  also
 #  therefore means  that it is reserved for developers  and  experienced
 #  professionals having in-depth computer knowledge. Users are therefore
 #  encouraged to load and test the software's suitability as regards their
 #  requirements in conditions enabling the security of their systems and/or
 #  data to be ensured and,  more generally, to use and operate it in the
 #  same conditions as regards security.
 #
 #  The fact that you are presently reading this means that you have had
 #  knowledge of the CeCILL-C license and that you accept its terms.
 #
*/

#ifndef _SMALLMATRIX_RESULTS_H
#define _SMALLMATRIX_RESULTS_H

#include <SmallMatrix/SmallMatrix.h>

NAMESPACE_BEGIN(SmallMatrix)

template <int M, int N, int SYM = 0>
struct DimToType {
	typedef Matrix<M,N> type;
};
template <int N>
struct DimToType<N,N,0> {
	typedef SquareMatrix<N> type;
};
template <int N>
struct DimToType<N,N,1> {
	typedef SymMatrix<N> type;
};
template <int N>
struct DimToType<N,1,0> {
	typedef Vector<N> type;
};
template <>
struct DimToType<1,1, 0> {
	typedef reel type;
};
template <>
struct DimToType<1,1, 1> {
	typedef reel type;
};

template <typename T1,typename T2> struct SameType { enum { OK = false }; };
template <typename T> struct SameType<T,T> { enum { OK = true }; };

template <int Functor_Type, typename T1, typename T2>
struct Dimensions;
template <typename T1, typename T2>
struct Dimensions<FUNCTOR_MULTIPLICATIVE, T1, T2> {
	//typedef Dimensions<1, T1, T2> Dims;
	//typedef SameType<T1,T2> ST;
	typedef DescriptionTraits<T1> Descriptor1;
	typedef DescriptionTraits<T2> Descriptor2;
	enum { ROW = typename Descriptor1::SIZE == 1 ? typename Descriptor2::ROW : typename Descriptor1::ROW };
	enum { COL = typename Descriptor2::SIZE == 1 ? typename Descriptor1::COL : typename Descriptor2::COL };
	enum { SYM = 0 };
	enum { OK = ((typename Descriptor1::COL)==(typename Descriptor2::ROW)) || (typename Descriptor1::SIZE == 1) || (typename Descriptor2::SIZE == 1) };
	typedef typename DimToType<ROW,COL,SYM>::type type;
};
template <typename T1, typename T2>
struct Dimensions<FUNCTOR_ADDITIVE, T1, T2> {
	typedef DescriptionTraits<T1> Descriptor1;
	typedef DescriptionTraits<T2> Descriptor2;
	enum { ROW = typename Descriptor1::SIZE == 1 ? typename Descriptor2::ROW : typename Descriptor1::ROW };
	enum { COL = typename Descriptor1::SIZE == 1 ? typename Descriptor2::COL : typename Descriptor1::COL };
	enum { SYM = (typename Descriptor1::SYM == 1) && (typename Descriptor2::SYM == 1) ? 1 : 0 };
	enum { OK = (((typename Descriptor1::COL)==(typename Descriptor2::COL)) && ((typename Descriptor1::ROW)==(typename Descriptor2::ROW))) || (typename Descriptor1::SIZE == 1) || (typename Descriptor2::SIZE == 1) };
	typedef typename DimToType<ROW,COL,SYM>::type type;
};
template <typename T1>
struct Dimensions<FUNCTOR_DEFAULT, T1, void> {
	typedef DescriptionTraits<T1> Descriptor1;
	enum { ROW = typename Descriptor1::ROW };
	enum { COL = typename Descriptor1::COL };
	enum { SYM = typename Descriptor1::SYM };
	enum { OK = true };
	typedef typename DimToType<ROW,COL,SYM>::type type;
};
template <typename T1, typename T2>
struct Dimensions<FUNCTOR_DEFAULT, T1, T2> {
	typedef DescriptionTraits<T2> Descriptor2;
	enum { ROW = typename Descriptor2::ROW };
	enum { COL = typename Descriptor2::COL };
	enum { SYM = typename Descriptor2::SYM };
	enum { OK = true };
	typedef typename DimToType<ROW,COL,SYM>::type type;
};
template <typename T1, typename T2>
struct Dimensions<FUNCTOR_ASSIGN, T1, T2> {
	typedef DescriptionTraits<T1> Descriptor1;
	typedef DescriptionTraits<T2> Descriptor2;
	enum { OK = ( ((typename Descriptor1::COL)==(typename Descriptor2::COL)) && ((typename Descriptor1::ROW)==(typename Descriptor2::ROW)) && ((typename Descriptor1::ELEVATION) <= (typename Descriptor2::ELEVATION))) || (typename Descriptor2::SIZE == 1) };
	typedef typename T1 type;
};

template <typename T>
struct ResultFromPreviousExpression {
	typedef typename T::result_type result_type;
};
template <>
struct ResultFromPreviousExpression<reel> {
	typedef reel result_type;
};
template <>
struct ResultFromPreviousExpression<void> {
	typedef void result_type;
};

template <int Functor_Type, typename T1, typename T2>
struct Results {
	typedef typename ResultFromPreviousExpression<T1>::result_type Left;
	typedef typename ResultFromPreviousExpression<T2>::result_type Right;
	typedef Dimensions<Functor_Type, Left, Right> Dims;
	enum { OK = typename Dims::OK };
	typedef typename Dims::type type;
};

NAMESPACE_END

#endif
